<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="125px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="queryParams.name" placeholder="请输入姓名" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="年级" prop="level">
        <el-input v-model="queryParams.level" placeholder="请输入年级" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="联系方式" prop="phone">
        <el-input v-model="queryParams.phone" placeholder="请输入手机号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="课程分类" prop="classTypeName">
        <el-input v-model="queryParams.classTypeName" placeholder="请输入课程分类" clearable
          @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5" v-if="!this.queryParams.recPhone">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" :disabled="multiple" @click="openLearn"
          v-hasPermi="['course:Student:remove']">学管分配</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 交付中心学员列表 -->
    <el-table v-if="!this.queryParams.recPhone && !isPayCenter" v-loading="loading" :data="StudentList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学员编号" align="center" prop="code" width="100px" />
      <el-table-column label="姓名" align="center" prop="name" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="年级" align="center" prop="level" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="推荐手机号" align="center" prop="recPhone" />
      <el-table-column label="交付小组" align="center" prop="payGroupName" />
      <el-table-column label="首次上课时间" align="center" prop="firstTime" width="100px" />
      <el-table-column label="创建时间" align="center" prop="createTime" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="250px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="planClass(scope.row, 0)">排课</el-button>
          <el-button size="mini" type="text" @click="planClass(scope.row)">课程表</el-button>
          <el-button size="mini" type="text" v-if="isTest == '1'" @click="planClass(scope.row, 1)">正式课报告</el-button>
          <el-button size="mini" type="text" v-if="isTest == '1'" @click="toTzplanClass(scope.row)">试课报告</el-button>
          <el-button size="mini" type="text" v-if="isTest == '0'" @click="planClass(scope.row, 1)">试课报告</el-button>
          <el-button size="mini" type="text" @click="planClass(scope.row, 3)">派单记录</el-button>
          <el-button size="mini" type="text" @click="planClass(scope.row, 2)">上课信息对接</el-button>
          <el-button size="mini" type="text" v-if="isTest == '1'" @click="planClass(scope.row, 4)">家长反馈</el-button>
          <el-button size="mini" type="text" @click="planClass(scope.row, 6)">更换教练</el-button>
        </template>
      </el-table-column>
      <el-table-column label="学管师" align="center" prop="learnName" />
      <el-table-column label="上课教练" align="center" prop="coachName" />
      <el-table-column label="授课方式" align="center" prop="teachMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_teach_method" :value="scope.row.teachMethod" />
        </template>
      </el-table-column>
      <el-table-column label="岗位类型" align="center" prop="position">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.c_position" :value="scope.row.position" />
        </template>
      </el-table-column>
      <el-table-column label="所属学段" align="center" prop="teachSection">
        <template slot-scope="scope">
          {{ scope.row.teachSection == '0' ? "小学" : (scope.row.teachSection == '1' ? "初中" : "高中") }}
        </template>
      </el-table-column>
      <el-table-column label="课程等级" align="center" prop="classLevel">
        <template slot-scope="scope">
          {{ scope.row.classLevel == '0' ? "一级" : "二级" }}
        </template>
      </el-table-column>
      <el-table-column label="课程名称" align="center" prop="className" />
      <el-table-column label="课程分类" align="center" prop="classTypeName" />
      <el-table-column label="已购学时" align="center" prop="buyLearnTime" />
      <el-table-column label="交付学时" align="center" prop="payLearnTime" />
      <el-table-column label="已退学时" align="center" prop="retreatLearnTime" />
      <el-table-column label="剩余学时" align="center" prop="surplusLearnTime" />
      <el-table-column label="门店编号" align="center" prop="storeCode" width="100px" />
      <el-table-column label="门店名称" align="center" prop="storeName" />
      <el-table-column label="支付状态" align="center" prop="isPay">
        <template slot-scope="scope">
          <span v-if="scope.row.isPay === '1'">已支付</span>
          <span v-else>未支付</span>
        </template>
      </el-table-column>
    </el-table>
    <!-- 合伙人学员列表 -->
    <el-table v-if="this.queryParams.recPhone && !isPayCenter" v-loading="loading" :data="StudentList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学员姓名" align="center" prop="name" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="年级" align="center" prop="level" />
      <el-table-column label="已购学时" align="center" prop="buyLearnTime" />
      <el-table-column label="剩余学时" align="center" prop="surplusLearnTime" />
    </el-table>
    <!-- 合伙人集中交付学员列表 -->
    <el-table v-if="!this.queryParams.recPhone && isPayCenter" v-loading="loading" :data="StudentList"
      @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="学员姓名" align="center" prop="name" />
      <el-table-column label="联系方式" align="center" prop="phone" />
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_user_sex" :value="scope.row.sex" />
        </template>
      </el-table-column>
      <el-table-column label="年级" align="center" prop="level" />
      <el-table-column label="已购学时" align="center" prop="buyLearnTime" />
      <el-table-column label="剩余学时" align="center" prop="surplusLearnTime" />
      <el-table-column label="课程分类" align="center" prop="classTypeName" />
      <el-table-column label="首次上课时间" align="center" prop="firstTime" width="100px" />
      <el-table-column label="所属交付小组" align="center" prop="payGroupName" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="200px">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="planClass(scope.row)">课程表</el-button>
          <el-button size="mini" type="text" @click="planClass(scope.row, 2)">上课信息对接</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

    <!-- 学管分配 -->
    <el-dialog title="学管分配" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" label-width="125px">
        <el-form-item label="学员数量">
          <span>{{ ids.length }}</span>
        </el-form-item>
        <el-form-item label="学员名称">
          <span>{{ names }}</span>
        </el-form-item>
        <el-form-item label="学管师" prop="learnId">
          <el-select v-model="learnId" placeholder="请选择学管师" filterable size="large" style="width: 240px">
            <el-option v-for="item in LearnList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="setLearnId()">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
    <!-- 排课 -->
    <el-dialog :title="title" :visible.sync="classOpen" width="800px" append-to-body>
      <el-form ref="classForm" :model="classForm" :rules="rulesClass" label-width="125px">
        <el-form-item label="学员编号" prop="stuCode">
          <el-input v-model="classForm.stuCode" placeholder="请输入学员编号" disabled />
        </el-form-item>
        <el-form-item label="学员姓名" prop="stuName">
          <el-input v-model="classForm.stuName" placeholder="请输入学员姓名" disabled />
        </el-form-item>
        <el-form-item label="课程名称" prop="className">
          <el-input v-model="classForm.className" placeholder="请输入课程名称" disabled />
        </el-form-item>
        <el-form-item label="课程分类" prop="classTypeName">
          <el-input v-model="classForm.classTypeName" placeholder="请输入课程分类名称" disabled />
        </el-form-item>
        <el-form-item label="学管姓名" prop="leaName">
          <el-input v-model="classForm.leaName" placeholder="学管姓名" disabled />
        </el-form-item>
        <el-form-item label="教练姓名" prop="coachName">
          <el-input v-model="classForm.coachName" placeholder="教练姓名" disabled />
        </el-form-item>
        <el-form-item label="授课方式" prop="teachMethod">
          <el-radio-group @change="teachMethodChange" v-model="classForm.teachMethod">
            <el-radio v-for="dict in dict.type.c_teach_method" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="上课日期" prop="classTime">
          <el-date-picker v-model="classForm.classTime" format="yyyy/MM/dd" value-format="yyyy-MM-dd" type="date"
            placeholder="请选择上课日期" size="large" />
        </el-form-item>
        <el-form-item label="开始时间" prop="starTime">
          <el-time-select v-model="classForm.starTime" style="width: 240px" :max-time="classForm.endTime" class="mr-4"
            placeholder="请选择开始时间" start="08:30" step="00:15" end="18:30" />
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-time-select v-model="classForm.endTime" style="width: 240px" :min-time="classForm.startTime"
            placeholder="请选择结束时间" start="08:30" step="00:15" end="18:30" />
        </el-form-item>
        <el-form-item label="所学课时" prop="learnNum">
          <el-input v-model="classForm.learnNum" style="width: 240px"  disabled>
            <template #suffix>
              <span>时</span>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitClassForm">确 定</el-button>
        <el-button @click="classCancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 试课报告 -->
    <el-dialog :title="title" :visible.sync="testOpen" width="800px" append-to-body>
      <el-form ref="classForm" :model="classForm" :rules="rules" label-width="125px">
        <el-form-item label="首次上课时间" prop="firstTime" label-width="125px">
          <el-input v-model="classForm.firstTime" disabled />
        </el-form-item>
        <el-form-item label="学员编号" prop="stuCode" label-width="125px">
          <el-input v-model="classForm.stuCode" disabled />
        </el-form-item>
        <el-form-item label="学员姓名" prop="stuName" label-width="125px">
          <el-input v-model="classForm.stuName" disabled />
        </el-form-item>
        <el-form-item label="年级" prop="level" label-width="125px">
          <el-input v-model="classForm.level" disabled />
        </el-form-item>
        <el-form-item label="所属学段" prop="teachSection" label-width="125px">
          <el-input :value="classForm.teachSection==0?'小学':(classForm.teachSection==1?'初中':'高中')" disabled />
        </el-form-item>
        <el-form-item label="教练" prop="coachName" label-width="125px">
          <el-input v-model="classForm.coachName" disabled />
        </el-form-item>
        <el-form-item label="学习学时" prop="level" label-width="125px">
          <el-input v-model="classForm.payLearnTime" disabled />
        </el-form-item>
        <el-form-item label="学习状况反馈" prop="level" label-width="125px">
          <el-input v-model="classForm.learnRemark" disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button @click="testOpen = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 上课信息对接 -->
    <el-dialog :title="title" :visible.sync="upClassOpen" width="800px" append-to-body>
      <el-form ref="classForm" :model="classForm" :rules="rules" label-width="125px">
        <el-form-item label="创建时间" prop="createTime" label-width="125px">
          <el-input v-model="classForm.createTime" disabled />
        </el-form-item>
        <el-form-item label="学员编号" prop="stuCode" label-width="125px">
          <el-input v-model="classForm.stuCode" disabled />
        </el-form-item>
        <el-form-item label="学员姓名" prop="stuName" label-width="125px">
          <el-input v-model="classForm.stuName" disabled />
        </el-form-item>
        <el-form-item label="联系方式" prop="phone" label-width="125px">
          <el-input v-model="classForm.phone" disabled />
        </el-form-item>
        <el-form-item label="课程分类" prop="classTypeName" label-width="125px">
          <el-input v-model="classForm.classTypeName" disabled />
        </el-form-item>
        <el-form-item label="推荐人手机号" prop="recPhone" label-width="125px">
          <el-input v-model="classForm.recPhone" disabled />
        </el-form-item>
        <el-form-item label="年级" prop="level" label-width="125px">
          <el-input v-model="classForm.level" disabled />
        </el-form-item>
        <el-form-item label="学习学时" prop="buyLearnTime" label-width="125px">
          <el-input v-model="classForm.buyLearnTime" disabled />
        </el-form-item>
        <el-form-item label="首次上课时间" prop="firstTime" label-width="125px">
          <el-input v-model="classForm.firstTime" disabled />
        </el-form-item>
        <el-form-item label="教练" prop="coachName" label-width="125px">
          <el-input v-model="classForm.coachName" disabled />
        </el-form-item>
        <el-form-item label="是否试课" prop="isTest" label-width="125px">
          <el-radio-group v-model="classForm.isTest" disabled>
            <el-radio v-for="dict in dict.type.course_yes_no" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="是否新生" prop="isNew" label-width="125px">
          <el-radio-group v-model="classForm.isNew" disabled>
            <el-radio v-for="dict in dict.type.course_yes_no" :key="dict.value" :label="dict.value">{{ dict.label
              }}</el-radio>
          </el-radio-group>
        </el-form-item>

        <el-form-item label="教材版本" prop="jcVersion" label-width="125px">
          <el-input v-model="classForm.jcVersion" disabled />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button @click="upClassOpen = false">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 派单记录 -->
    <el-dialog :title="title" :visible.sync="disOpen" width="800px" append-to-body>
      <el-timeline style="max-width: 600px">
        <el-timeline-item v-for="(item, index) in DispatchList" :key="index" :timestamp="item.disTime" placement="top">
          <el-card>
            <h4>到达交付中心：{{ item.centerName || '无交付中心' }}</h4>
            <p>当前派单小组：{{ item.payGroupName || '无交付小组' }}</p>
            <p>备注：{{ item.remark || '无备注' }}</p>
          </el-card>
        </el-timeline-item>
        <!-- <el-timeline-item timestamp="2018/4/12" placement="top">
          <el-card>
            <h4>Update Github template</h4>
            <p>Tom committed 2018/4/12 20:46</p>
          </el-card>
        </el-timeline-item> -->
      </el-timeline>
      <div slot="footer" class="dialog-footer" style="text-align: center;">
        <el-button @click="disOpen = false">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 更换教练 -->
    <el-dialog :title="title" :visible.sync="changeCoachOpen" width="500px" append-to-body>
      <el-form ref="changeCoachForm" :model="classForm" label-width="125px">
        <el-form-item label="学员编号" prop="stuCode">
          <el-input v-model="classForm.stuCode" placeholder="请输入学员编号" disabled />
        </el-form-item>
        <el-form-item label="学员姓名" prop="stuName">
          <el-input v-model="classForm.stuName" placeholder="请输入学员姓名" disabled />
        </el-form-item>
        <el-form-item label="原教练" prop="coachName">
          <el-input v-model="classForm.coachName" placeholder="原教练" disabled />
        </el-form-item>
        <el-form-item label="新教练" prop="newCoachId">
          <el-select v-model="classForm.newCoachId" placeholder="请选择教练" filterable size="large" style="width: 100%">
            <el-option v-for="item in CoachList" :key="item.id" :label="item.name" :value="item.id" />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitChangeCoach">确 定</el-button>
        <el-button @click="changeCoachOpen = false">取 消</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
import { listStudent, allStudent, setLearnId, getStudent, delStudent, addStudent, updateStudent } from "@/api/course/Student";
import { allCoach } from "@/api/course/Coach";
import { listPlanClass, getPlanClass, delPlanClass, addPlanClass, updatePlanClass } from "@/api/course/planClass";
import { listDispatch, getDispatch, delDispatch, addDispatch, updateDispatch } from "@/api/course/Dispatch";
import store from '@/store'


export default {
  name: "Student",
  dicts: ['course_yes_no', 'c_is_use', 'c_position', 'sys_user_sex', 'c_teach_method'],
  data() {
    return {
      // 遮罩层
      loading: true,
      isPayCenter: null,
      // 选中数组
      ids: [],
      payGroupIds: [],
      changeCoachOpen :false,
      // 选中学员名称数组
      names: [],
      // 学管师id
      learnId: "",
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 学员信息表格数据
      StudentList: [],
      // 学管师列表
      LearnList: [],
      //教练列表
      CoachList: [],
      // 派单记录表格数据
      DispatchList: [],
      // 弹出层标题
      title: "",
      //是否试课：0试课  1正式课
      isTest: "",
      // 是否显示弹出层
      // 学管分配弹出层
      open: false,
      //排课弹出层
      classOpen: false,
      //试课弹出层
      testOpen: false,
      // 派单记录弹出层
      disOpen: false,
      // 上课信息对接弹出层
      upClassOpen: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        code: null,
        name: null,
        sex: null,
        level: null,
        phone: null,
        recPhone: null,
        storeUserId: null,
        storeCode: null,
        storeName: null,
        firstTime: null,
        isAddGroup: null,
        learnId: null,
        coachId: null,
        buyLearnTime: null,
        payLearnTime: null,
        retreatLearnTime: null,
        surplusLearnTime: null,
        className: null,
        classTypeId: null,
        classTypeName: null,
        expectTime: null,
        orderType: null,
        orderCode: null,
        payAmount: null,
        isPay: null,
        learnRemark: null,
        payGroupId: null,
        isTest: null,
        isNew: null,
        isUse: null,
        userId:null,
      },
      // 表单参数
      form: {},
      classForm: {},
      // 表单校验
      rules: {
      },
      // 表单校验
      rulesClass: {
        teachMethod: [{ required: true, message: "请选择授课方式", trigger: "change" }],
        classTime: [{ required: true, message: "请选择上课日期", trigger: "change" }],
        starTime: [{ required: true, message: "请选择开始时间", trigger: "change" }],
        endTime: [{ required: true, message: "请选择结束时间", trigger: "change" }],
        learnNum: [{ required: true, message: "请输入所学课时", trigger: "blur" }]
      }
    };
  },
  created() {
    const userId = this.$route.query.userId;
    if(userId){
      this.queryParams.userId=userId;
    }
    this.getList();
  },
  methods: {
    /** 查询学员信息列表 */
    getList() {
      this.loading = true;
      //合伙人集中交付学员列表
      this.isPayCenter = this.$route.query.isPayCenter;
      console.log('[ isPayCenter ] >', this.isPayCenter)


      //合伙人学员列表查询参数配置
      const isPhone = this.$route.query.isPhone;
      if (isPhone) {
        const recPhone = store.getters.phone;
        // 获取用户名称
        const name = store.getters.user.name;
        // console.log('[ recPhone ] >', recPhone)
        if(name!='admin'){
          this.queryParams.recPhone = recPhone;
        }


      }
      //是否试课：0试课  1正式课
      this.isTest = this.$route.query.isTest;
      // console.log('[ isTest ] >', isTest)
      this.queryParams.isTest = this.isTest;
      if (this.isPayCenter || isPhone) {
        // 合伙人学员列表
        // allStudent(this.queryParams).then(response => {
          listStudent(this.queryParams).then(response => {

          this.StudentList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      } else {
        // 交付中心获取学员信息列表
        listStudent(this.queryParams).then(response => {
          this.StudentList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      }
      // 获取学管师列表
      allCoach({ roleType: '2' }).then(response => {
        this.LearnList = response.data;
        console.log('[ response ] >', response)
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 取消按钮
    classCancel() {
      this.classOpen = false;
      // this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        code: null,
        name: null,
        sex: null,
        level: null,
        phone: null,
        recPhone: null,
        storeUserId: null,
        storeCode: null,
        storeName: null,
        firstTime: null,
        isAddGroup: null,
        learnId: null,
        coachId: null,
        buyLearnTime: null,
        payLearnTime: null,
        retreatLearnTime: null,
        surplusLearnTime: null,
        className: null,
        classTypeId: null,
        classTypeName: null,
        expectTime: null,
        orderType: null,
        orderCode: null,
        payAmount: null,
        isPay: null,
        learnRemark: null,
        payGroupId: null,
        isTest: null,
        isNew: null,
        isUse: null,
        remark: null,
        createBy: null,
        createTime: null,
        updateTime: null,
        delFlag: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.payGroupIds = selection.map(item => item.payGroupId)
      this.names = selection.map(item => item.name)
      this.single = selection.length !== 1
      this.multiple = !selection.length
      console.log('[ payGroupIds ] >', this.payGroupIds)
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加学员信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getStudent(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改学员信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateStudent(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addStudent(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    toTzplanClass(row){
        this.$router.push('/test/testStudent?isTest=0&userId=' + row.userId);
    },
    //val:0排课  1试课报告 2上课信息对接 3派单记录
    planClass(row, val) {
      console.log('[ row ] >', row)
      this.classForm = {
        stuId: row.id,
        orderCode: row.orderCode,
        linkPhone: row.linkPhone,
        storeUserId: row.storeUserId,
        storeCode: row.storeCode,
        storeName: row.storeName,
        stuCode: row.code,
        stuName: row.name,
        phone: row.phone,
        recPhone: row.recPhone,
        className: row.className,
        classTypeId: row.classTypeId,
        classTypeName: row.classTypeName,
        leaId: row.learnId,
        leaName: row.learnName,
        coaId: row.coachId,
        coaName: row.coachName,
        payGroupId: row.payGroupId,
        level: row.level,
        firstTime: row.firstTime,
        payLearnTime: row.payLearnTime,
        buyLearnTime: row.buyLearnTime,
        createTime: row.createTime,
        orderType: row.isTest,
        isNew: row.isNew,
        teachSection: row.teachSection,
        classLevel: row.classLevel,
        learnRemark: row.learnRemark,
        coachName: row.coachName,
        isTest: row.isTest,
        classTime: null, // 上课日期
        starTime: null, // 开始时间
        endTime: null, // 结束时间
        learnNum: 1, // 共计学时
        teachMethod: row.teachMethod, // 授课方式
      };
      // 获取所有教练列表
      // allCoach({ payGroupId: row.payGroupId, teachMethod: row.teachMethod, teachSection: row.teachSection }).then(response => {
      //   this.CoachList = response.data;
      //   console.log('[ response ] >', response)
      // });
      const name = this.$store.getters.name;
      var   pare={ payGroupId: row.payGroupId, roleType: '0' }
      if(name =="admin"){
        pare={  roleType: '0' }
      }
      console.log('[ name ] >', name)
      // 如果账号是admin，查询所有教练 判断一下
      allCoach(pare).then(response => {
        this.CoachList = response.data;
        console.log('[ response ] >', response)
      });

      if (val === 0) {
        // 排课
        this.title = "添加排课信息";
        this.classOpen = true;
      } else if (val === 1) {
        // 试课报告
        this.testOpen = true;
        this.title = row.className + "反馈结果";
        console.log('[ 试课 ] >', this.classForm)
      } else if (val === 2) {
        // 上课信息对接
        this.upClassOpen = true;
        this.title = "上课信息对接";
      } else if (val === 3) {
        // 派单记录
        this.disOpen = true;
        this.title = "派单记录";
        listDispatch({ pageNum: 1, pageSize: 999, orderCode: row.orderCode }).then(response => {
          this.DispatchList = response.rows;
        });
      }else if (val === 4) {
        // 家长反馈
        this.testOpen = true;
        this.title = row.className + "家长反馈结果";
        this.classForm.learnRemark = row.userRemark
      }  else if (val === 6) {
        // 更换教练
        this.title = "更换教练";
        this.changeCoachOpen = true;
      } else{
        // 课程表
        if(this.isTest=='0'){
          this.$router.push('/test/planClass1?stuId=' + row.id);
        }else{
          this.$router.push('/formal/planClass?stuId=' + row.id);
        }
      }
    },
    teachMethodChange(val) {
      // 获取所有教练列表
      allCoach({ payGroupId: this.classForm.payGroupId, teachMethod: val }).then(response => {
        this.CoachList = response.data;
        console.log('[ response ] >', response)
      });
    },
    calculateTime() {
      const starTime = this.classForm.starTime
      const endTime = this.classForm.endTime
      let diffHours = 0;
      if (starTime && endTime) {
        // 假设时间格式为 "HH:mm"
        const [sh, sm] = starTime.split(':').map(Number);
        const [eh, em] = endTime.split(':').map(Number);
        const start = sh * 60 + sm;
        const end = eh * 60 + em;
        diffHours = (end - start) / 60;
        // 保留一位小数
        diffHours = Math.round(diffHours * 10) / 10;
      }
      console.log('相差小时数:', diffHours);
      this.classForm.learnNum = diffHours; // 将学时转换为字符串
    },
    submitClassForm() {
      if (this.classForm.payGroupId == null || this.classForm.payGroupId == '') {
        this.$modal.msgError("请先去接单管理派单后再进行排课操作！");
        this.classOpen = false;
        return;
      }
      if (this.classForm.leaId == null || this.classForm.leaId == '') {
        this.$modal.msgError("请先去分配学管后再进行排课操作！");
        this.classOpen = false;
        return;
      }
      //是否试课：0试课  1正式课
      this.classForm.orderType = this.isTest;
      console.log('[ classForm ] >', this.classForm);
      this.$refs["classForm"].validate(valid => {
        if (valid) {
          if (this.classForm.id != null) {
            updatePlanClass(this.classForm).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.classOpen = false;
              this.getList();
            });
          } else {
            addPlanClass(this.classForm).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.classOpen = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 学管分配按钮操作 */
    openLearn() {
      this.open = true;
      this.learnId = "";
      this.names = this.names.join(',');
    },
    /** 学管分配 */
    setLearnId() {
      if (this.payGroupIds.includes(null)) {
        this.$modal.msgError("请先去接单管理派单后再进行学管分配操作！");
        this.open = false;
        return;
      }
      setLearnId(this.ids, this.learnId).then(() => {
        this.getList();
        this.$modal.msgSuccess("学管分配成功");
        this.open = false;
      }).catch(() => { });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除学员信息编号为"' + ids + '"的数据项？').then(function () {
        return delStudent(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('course/Student/export', {
        ...this.queryParams
      }, `Student_${new Date().getTime()}.xlsx`)
    },
    /** 提交更换教练 */
    submitChangeCoach() {
      if (!this.classForm.newCoachId) {
        this.$modal.msgError("请选择新教练");
        return;
      }

      const studentData = {
        id: this.classForm.stuId,
        coachId: this.classForm.newCoachId
      };
      updateStudent(studentData).then(response => {
        this.$modal.msgSuccess("更换教练成功");
        this.changeCoachOpen = false;
        this.getList();
      }).catch(error => {
        console.error(error);
        this.$modal.msgError("更换教练失败");
      });
    },
  }
};
</script>
